﻿
Partial Class WebForm_ManageStaff_AddStaff
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Page.IsPostBack Then

        End If
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindDDLActive()
        Me.BindDDLGender()
        Me.BindDDLOrganization()

        If Me.fv.CurrentMode = FormViewMode.Insert Then

        ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then

            If Not DataBinder.Eval(fv.DataItem, "Gender") Is DBNull.Value Then
                Me.fv.FindDDLControl("ddlGender").SelectedValue = DataBinder.Eval(fv.DataItem, "Gender")
            End If

            If Not DataBinder.Eval(fv.DataItem, "OrganizationID") Is DBNull.Value Then
                Me.fv.FindDDLControl("ddlOrganization").SelectedValue = DataBinder.Eval(fv.DataItem, "OrganizationID")
            End If

            If Not DataBinder.Eval(fv.DataItem, "Active") Is DBNull.Value Then
                Me.fv.FindDDLControl("ddlActive").SelectedValue = DataBinder.Eval(fv.DataItem, "Active")
            End If

            Me.fv.FindTxtControl("txtWindowsID").Enabled = False
        End If
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.Save() Then
                MyObj.ShowMessage("Save Successful!")
                RaiseEvent Saved(Me, New EventArgs)
            Else
                MyObj.ShowMessage("Your ID has existed!")
            End If

        ElseIf e.CommandName = "btnCancel" Then
            RaiseEvent Canceled(Me, New EventArgs)
        End If
    End Sub

End Class

Partial Class WebForm_ManageStaff_AddStaff

    ''' <summary>
    ''' 新增模式
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Show()
        Me.DataAccessMode = My.DataAccessMode.Insert

        If Me.fv.CurrentMode = FormViewMode.Insert Then
            Me.fv.DataBind()
        Else
            Me.fv.ChangeMode(FormViewMode.Insert)
        End If

        Me.Visible = True
    End Sub

    ''' <summary>
    ''' 編輯模式
    ''' </summary>
    ''' <param name="employeeID"></param>
    ''' <remarks></remarks>
    Public Sub ShowEdit(ByVal employeeID As String)

        Me.fv.ChangeMode(FormViewMode.Edit)

        Me.BindDataForEdit(employeeID)

        Me.Visible = True
    End Sub

    Private Sub BindDataForEdit(ByVal employeeID As String)

        Me.EmployeeID = employeeID

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat(" Select e.* ")
        sql.AppendFormat("from Employee e ")
        sql.AppendFormat("where e.EmployeeID = @EmployeeID ")

        sql.AddWithValue("EmployeeID", employeeID)

        Dim dt As Data.DataTable = dat.GetData(sql)

        Me.fv.DataSource = dt
        Me.fv.DataBind()

    End Sub


    Private Function Save() As Boolean

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As New Data.DataTable
        sql.Length = 0

        Using scope As New Transactions.TransactionScope
            If Me.fv.CurrentMode = FormViewMode.Insert Then

                sql.Length = 0

                sql.AppendFormat("Select * from Employee ")
                sql.AppendFormat("where EmployeeID='{0}' ", Me.fv.FindTxtControl("txtWindowsID").Text)
                dt = dat.GetData(sql)

                If dt.Rows.Count > 0 Then
                    Return False
                Else
                    sql.Length = 0

                    sql.AppendFormat(" Insert Into Employee(EmployeeID,EmployeeName,OrganizationID,Gender,Title,Email,PhoneNumber,Ext,Password,Active) Select  ")
                    sql.AppendFormat(" '{0}', ", Me.fv.FindTxtControl("txtWindowsID").Text)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindTxtControl("txtEmployeeName").Text)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindDDLControl("ddlOrganization").SelectedValue)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindDDLControl("ddlGender").SelectedValue)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindTxtControl("txtTitle").Text)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindTxtControl("txtEmail").Text)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindTxtControl("txtPhoneNumber").Text)
                    sql.AppendFormat(" '{0}', ", Me.fv.FindTxtControl("txtExt").Text)
                    sql.AppendFormat(" '{0}', ", clsAuth.EncryptPassword(Me.fv.FindTxtControl("txtPassword").Text))
                    sql.AppendFormat(" '{0}' ", Me.fv.FindDDLControl("ddlActive").SelectedValue)

                End If
              
                dat.AccData(sql)
            ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then

                sql.Length = 0

                sql.AppendFormat(" Update Employee Set ")
                sql.AppendFormat(" EmployeeName='{0}', ", Me.fv.FindTxtControl("txtEmployeeName").Text)
                sql.AppendFormat(" OrganizationID='{0}', ", Me.fv.FindDDLControl("ddlOrganization").SelectedValue)
                sql.AppendFormat(" Gender='{0}', ", Me.fv.FindDDLControl("ddlGender").SelectedValue)
                sql.AppendFormat(" Title='{0}', ", Me.fv.FindTxtControl("txtTitle").Text)
                sql.AppendFormat(" Email='{0}', ", Me.fv.FindTxtControl("txtEmail").Text)
                sql.AppendFormat(" PhoneNumber='{0}', ", Me.fv.FindTxtControl("txtPhoneNumber").Text)
                sql.AppendFormat(" Ext='{0}', ", Me.fv.FindTxtControl("txtExt").Text)

                If Me.fv.FindTxtControl("txtPassword").Text <> "" Then
                    sql.AppendFormat(" Password='{0}', ", clsAuth.EncryptPassword(Me.fv.FindTxtControl("txtPassword").Text))
                End If

                sql.AppendFormat(" Active='{0}' ", Me.fv.FindDDLControl("ddlActive").SelectedValue)
                sql.AppendFormat(" Where EmployeeID='{0}'; ", Me.fv.FindTxtControl("txtWindowsID").Text)

                dat.AccData(sql)
            End If
            scope.Complete()
        End Using

        Return True

    End Function

#Region "資料繫結"
    Private Sub BindDDLActive()
        Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

        ddlActive.DataTextField = "Text"
        ddlActive.DataValueField = "Value"
        ddlActive.DataSource = MyObj.GetCodeMappingData("Organization", "Active")
        ddlActive.DataBind()
    End Sub

    Private Sub BindDDLGender()
        Dim ddlGender As DropDownList = TryCast(Me.fv.FindControl("ddlGender"), DropDownList)

        ddlGender.DataTextField = "Text"
        ddlGender.DataValueField = "Value"
        ddlGender.DataSource = MyObj.GetCodeMappingData("Employee", "Gender")
        ddlGender.DataBind()
        ddlGender.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

    End Sub

    Private Sub BindDDLOrganization()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select * From Organization Where Active='Y' ")

        Dim ddlOrganization As DropDownList = TryCast(Me.fv.FindControl("ddlOrganization"), DropDownList)

        ddlOrganization.DataTextField = "OrganizationName"
        ddlOrganization.DataValueField = "OrganizationID"
        ddlOrganization.DataSource = dat.GetData(sql)
        ddlOrganization.DataBind()

        ddlOrganization.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

    End Sub
#End Region

#Region "Event"
    Public Event Saved(ByVal source As Object, ByVal e As EventArgs)
    Public Event Canceled(ByVal source As Object, ByVal e As EventArgs)
#End Region

#Region "Property"
    Public Property EmployeeID() As String
        Get
            Return ViewState("EmployeeID")
        End Get
        Set(ByVal value As String)
            ViewState("EmployeeID") = value
        End Set
    End Property

    Private Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
#End Region
End Class
